package com.kkb.controller;

import com.kkb.pojo.Doctor;
import com.kkb.pojo.Hosregister;
import com.kkb.service.HosregisterService;
import com.kkb.vo.QueryVO;
import com.kkb.vo.ResultVO;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("hosregister")
@ResponseBody
/*@CrossOrigin*/
public class HosregisterController {
    @Resource
    private HosregisterService hosregisterService;
    //查询所有挂号信息
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ResultVO<Hosregister> queryByPage(Integer pageNum, Integer pageSize, QueryVO vo){
        if (pageNum == null || pageNum < 0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 0){
            pageSize = 5;
        }
        PageInfo<Hosregister> pageInfo = hosregisterService.queryByPage(pageNum, pageSize, vo);
        return new ResultVO<>(pageInfo);
    }
    //查询医生列表
    @RequestMapping(value = "show",method = RequestMethod.GET)
    public ResultVO<Doctor> show(){
        List<Doctor> show = hosregisterService.show();
        if (show.size()>0){
            return new ResultVO<>(show);
        }else {
            return new ResultVO<>(404,"数据丢失了");
        }
    }
    //挂号添加
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public ResultVO<Integer> add(Hosregister hos){
        hos.setHosR_time(new Date());
        hos.setHosR_state(0);
        int add = hosregisterService.add(hos);
        if (add>0){
            return new ResultVO<>();
        }else {
            return new ResultVO<>(500,"社保号已存在");
        }
    }
    //修改挂号
    @RequestMapping(value = "update",method = RequestMethod.PUT)
    public ResultVO<Integer> update(Hosregister hos){
        int update = hosregisterService.update(hos);
        if (update>0){
            return new ResultVO<>(200,"信息修改成功");
        }else {
            return new ResultVO<>(500,"社保号已存在");
        }
    }
    //退号批量操作
    @RequestMapping(value = "updateState",method = RequestMethod.GET)
    public ResultVO<Integer> updateState(@RequestParam(value = "ids",required = false) List<Integer> list){
        int i = hosregisterService.updateState(list);
        if ( i > 0 ){
            return new ResultVO<>(200,"退号成功");
        }else {
            return new ResultVO<>(500,"退号失败");
        }

    }
    //退号单个
    @RequestMapping(value = "{hosR_id}",method = RequestMethod.PATCH)
    public ResultVO<Integer> updateStateOne(@PathVariable(value = "hosR_id",required = false) Integer hosR_id){
        List<Integer> list = new ArrayList<>();
        list.add(hosR_id);
        int i = hosregisterService.updateState(list);
        if ( i > 0 ){
            return new ResultVO<>(200,"退号成功");
        }else {
            return new ResultVO<>(500,"退号失败");
        }
    }
    //查看详细信息
    @RequestMapping(value = "queryById/{hosR_id}",method = RequestMethod.GET)
    public ResultVO<Hosregister> queryById(@PathVariable("hosR_id") Integer hosR_id){
        Hosregister hosregister = hosregisterService.queryById(hosR_id);
        if (hosregister != null){
            return new ResultVO<>(hosregister);
        }
        return new ResultVO<>(404,"数据丢失了");
    }
}
